package com.xsj.controller;

import com.xsj.dto.ResponseDto;
import com.xsj.pojo.Card;
import com.xsj.service.CardService;

import com.xsj.vo.CardVO;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @BelongsProject: wisdom-restaurant-manage
 * @BelongsPackage: com.xsj.controller
 * @Author: tutu
 * @CreateTime: 2025-06-22 21:40
 * @Description: 用户卡券管理
 * @Version: 1.0
 */
@CrossOrigin
@RestController
@RequestMapping("/card")
public class CardController {

    @Autowired
    private CardService cardService;

    @GetMapping("/getCard")
    public ResponseDto getCard(@RequestParam(value = "userId", required = false) Integer userId) {

                return ResponseDto.success(cardService.getCard(userId));
    }


    @PostMapping("/addCard")
    public ResponseDto addCard(@RequestBody CardVO card) {
        try {
            Card addCard = cardService.addCard(card);

            return ResponseDto.success(addCard);
        }catch (Exception e){
            return ResponseDto.fail(4000,e.getMessage());
        }


    }

    @GetMapping("/havaCard")
    public ResponseDto havaCard(@RequestParam("userId") int userId,@RequestParam("cardId") int cardId) {
        return ResponseDto.success(cardService.haveCard(userId,cardId));
    }

    @GetMapping("/search")
    public ResponseDto search(@RequestParam("cardName")String cardName) {
        return ResponseDto.success(cardService.searchCard(cardName));
    }

    @GetMapping("/initializeStock")
    public ResponseDto initializeStock(@RequestParam("cardId") Integer cardId, @RequestParam("stock") Integer stock) {
        if (cardId == null || stock == null || stock < 0) {
            return ResponseDto.fail(4000,"参数错误");
        }

        try {
            cardService.initializeCouponStock(cardId, stock);
            return ResponseDto.success("优惠券 " + cardId + " 库存初始化成功，数量为: " + stock);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseDto.fail(41111,"库存初始化失败");
        }
    }

    @GetMapping("/getCardByCardId")
    public ResponseDto getCardByCardId(@RequestParam("userId") Integer userId) {
        return ResponseDto.success(cardService.getCardList(userId));
    }

    @GetMapping("/allCard")
    public ResponseDto allCard() {
        return ResponseDto.success(cardService.allCard());
    }
}
